package xcmg.device.infra.interceptor.externalApiCheck.handle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @author wangyong
 */
@Component
public class ApiCheckStrategy {

  private ApplicationContext applicationContext;
  @Autowired
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext=applicationContext;
  }

  public boolean checkHandle(HttpServletRequest request, HttpServletResponse response, ApiCheckOptionEnum optionEnum) {
    ApiCheckService service = (ApiCheckService)applicationContext.getBean(optionEnum.getBeanId());
    return service.doCheck(request, response);
  }

  /**
   * 外部api调用备件接口校验的方案
   *
   */
  public enum ApiCheckOptionEnum {

    /**
     * secret校验（secret+key混合时间等md5加密）
     */
    OPTION_SECRET("apiCheckOption1Service"),
    /**
     * secret混合接口参数的方式校验（secret+key混合时间以及接口参数名md5加密）
     */
    OPTION_SECRET_PARAM("apiCheckOption2Service");

    /**
     * 方案对应的bean
     */
    private String beanId;

    ApiCheckOptionEnum(String beanId) {
      this.beanId = beanId;
    }

    public String getBeanId() {
      return beanId;
    }

    public void setBeanId(String beanId) {
      this.beanId = beanId;
    }

    // 若有待续

  }


  public interface  ApiCheckService {

    /**
     * 处理校验
     *
     * @param request
     * @param response
     * @return
     */
    boolean doCheck(HttpServletRequest request, HttpServletResponse response);
  }

}
